import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/widgets/keep_alive.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';
import 'widget/item.dart';

class ExaminationListPage extends StatefulWidget {
  const ExaminationListPage({Key? key}) : super(key: key);

  @override
  State<ExaminationListPage> createState() => _ExaminationListPageState();
}

class _ExaminationListPageState extends State<ExaminationListPage> {
  final controller = Get.find<ExaminationListController>();
  final state = Get.find<ExaminationListController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: TDNavBar(
          backgroundColor: Colors.transparent,
          title: '在线考试',
        ),
        body: _buildBody(),
      ),
    );
  }

  _buildBody() {
    return Obx(() => state.firstInit
        ? const Center(child: CircularProgressIndicator())
        : Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TDTabBar(
          tabs: [
            for (var item in state.tabs)
              TDTab(
                text: item.label,
              )
          ],
          controller: controller.tabController,
          backgroundColor: Colors.transparent,
          showIndicator: true,
        ),
        Gaps.vGap8,
        Expanded(
            child: TabBarView(
              controller: controller.tabController,
              children: [
                for (var i = 0; i < state.tabs.length; i++)
                  _buildPage(state.tabs[i].value, i)
              ],
            ))
      ],
    ));
  }

  _buildPage(status, index) {
    return KeepAliveWrapper(
      child: ExaminationTabItemView(
        status: status,
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<ExaminationListController>();
    super.dispose();
  }
}